<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use think\Image;


class AdminMessageSortController extends AdminBaseController
{
    //列表
    public function index(){
        
        $log_list = Db::name('message_sort')->order('id desc')->select();
  
        $this->assign("data", $log_list);
        return $this->fetch();
    }
    //添加页面
    public function add(){
        return $this->fetch();
    }
    //添加提交
    public function addPost()
    {   
        
        if ($this->request->isPost()) {
            $post   = $this->request->param();
            
            if (empty($post['name'])) {
                $this->error("名称不能为空！");
            }
            $data = Db::name('message_sort')->where(['name'=>$post['name']])->find();
            if ($data) {
                $this->error("分类名称已存在");
            }
            $res = Db::name('message_sort')->insert($post);
            if ($res) {
                //操作日志
                admin_log($post['name'], 'add', '留言诉求分类');

                $this->success('添加成功!', url('AdminMessageSort/index'));
            }else{
                $this->error('添加失败');
            }
            
        }

    }
    //编辑
    public function edit(){
        $id = $this->request->param('id', 0, 'intval');
        $data = Db::name('message_sort')->find($id);
        $this->assign('result',$data);
        return $this->fetch();
    }
    //编辑提交
    public function editPost(){
        if ($this->request->isPost()) {
            $post   = $this->request->param();
            
            if (empty($post['name'])) {
                $this->error("名称不能为空！");
            }
            $data = Db::name('message_sort')->where(['name'=>$post['name']])->find();
            if ($data) {
                $this->error("分类名称已存在");
            }
            $res = Db::name('message_sort')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                //操作日志
                admin_log($post['name'], 'edit', '留言诉求分类');

                $this->success('编辑成功!', url('AdminMessageSort/index'));
            }else{
                $this->error('编辑失败');
            }
            
        }
    }
    //删除提交
    public function delete(){
        $param   = $this->request->param();
        if (isset($param['id'])) {
            $id   = $this->request->param('id', 0, 'intval');
            $data = Db::name('message_sort')->find($id);
            $res = Db::name('message_sort')->where(['id'=>$id])->delete();
            if ($res) {
                //操作日志
                admin_log($data['name'], 'delete', '留言诉求分类');

                $this->success('删除成功!', url('AdminMessageSort/index'));
            }else{
                $this->error('删除失败');
            }
        }
    }
    public function listOrder()
    {
        parent::listOrders(Db::name('message_sort'));

        //操作日志
        admin_log('', 'edit', '留言诉求分类排序');

        $this->success("排序更新成功！", '');
    }
}
